/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package tk.zater.File;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import tk.zater.obj.CS;
import tk.zater.obj.Properoutputer;
import tk.zater.reflect.ShowClass;

/**
 *创建properties和proterties.sample文件，sample文件可以为中文
 * @author zater
 */
public class CreateFile {

    public void CreatePropFile(String ClassName, String fileName) throws ClassNotFoundException, FileNotFoundException, IOException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException, Exception {
        Class<?> objectClass = Class.forName(ClassName);
        FileOutputStream fs = new FileOutputStream(new File(fileName + ".properties"));
        FileWriter fscs = new FileWriter(new File(fileName + ".properties.sample"));
        fscs.write("#这是一个可以含有中文的样例档案哦！，你可以依靠参考中文来修改那个英文原档案哦，\n#如果原档案存在问题，这个也是一个可以使用的档案哦，记得把这边的全部中文删去，包括这几行哦\n#作者留");
        fscs.write("\n");
        Properoutputer proper = new Properoutputer();
        Field[] objectFields = objectClass.getDeclaredFields();
        for (int i = 0; i < objectFields.length; i++) {
            String CSName = objectFields[i].getName();
            Method cs = objectClass.getMethod(createGetterFunction(CSName));
            Object temp = null;
            
            temp = cs.invoke(objectClass.newInstance());
            System.out.println(new ShowClass().backup(ClassName, objectClass.newInstance()));
            if (temp instanceof CS) {
                
                CS<?> CSObject = (CS) temp;
                fscs.write("#" + CSObject.note);
                fscs.write("\n");
                fscs.write(CSName + "=" + CSObject.getDvalue());
                fscs.write("\n");
                proper.store(fs, CSObject.note, 0);
                proper.store(fs, CSName + "=" + CSObject.getDvalue(), 1);

            }
        }
        fscs.close();
    }

    private String createGetterFunction(String Name) {
        StringBuffer buf = new StringBuffer("get");
        if (String.valueOf(Name.charAt(0)).matches("[a-z]")) {
            buf.append((char) ((int) Name.charAt(0) - 32));
        }
        buf.append(Name.substring(1));
        return buf.toString();
    }
}
